package sql;

import java.util.ArrayList;
import java.util.List;

public class Transaction implements SQLOperate {

    private final List<SQLOperate> sqls;

    public Transaction(int initCapital) {
        if (initCapital < 0) {
            this.sqls = new ArrayList<>();
        } else {
            this.sqls = new ArrayList<>(initCapital);
        }
    }

    public void add(SQLOperate sql) {
        sqls.add(sql);
    }

    @Override
    public void execute() {
        System.out.println("启动事务");
        for (SQLOperate sql : sqls) {
            sql.execute();
        }
        System.out.println("提交事务");
    }

    @Override
    public String getSQL() {
        StringBuilder sb = new StringBuilder();
        for (SQLOperate sql : sqls) {
            sb.append(sql.getSQL()).append("\n");
        }
        return sb.toString();
    }
}
